﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "StructureColumnHeightParameterUpdater.h"
#include "DbObjectUtils.h"
#include "GbmpBuiltInCategoryUniIdentitiesNew.h"
#include "GbmpBuiltInParameterDefinitions.h"
#include "IElement.h"
#include "IElementParameters.h"
#include "IElementPosition.h"
#include "IParameter.h"
#include "IParameterDefinitionLibrary.h"
#include "IPositionTwoAssociatedPlanes.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;

DBOBJECT_DATA_DEFINE(StructureColumnHeightParameterUpdater)
{
}

gcmp::OwnerPtr<gcmp::IInstanceFamilyParametersUpdater> gcmp::StructureColumnHeightParameterUpdater::Clone() const
{
    return NEW_AS_OWNER_PTR(StructureColumnHeightParameterUpdater, *this);
}

bool gcmp::StructureColumnHeightParameterUpdater::UpdateFamilyParametersToPositionParameters(IElement *pElement)
{
    return true;
}

bool gcmp::StructureColumnHeightParameterUpdater::UpdatePositionParametersToFamilyParameters(IElement *pElement)
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pElement != nullptr, L"无效pElement",L"GDMPLab",L"2024-03-30");
    IElementPosition *posBehavior = pElement->GetElementPosition();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(posBehavior != nullptr, L"无效posBehavior",L"GDMPLab",L"2024-03-30");

    const IPositionTwoAssociatedPlanes *pTwoAssociatedPlane = dynamic_cast<const IPositionTwoAssociatedPlanes *>(posBehavior->GetPositionAssociatedPlane());
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pTwoAssociatedPlane, L"无法读取高度参数",L"GDMPLab",L"2024-03-30");

    if (ElementId::InvalidID == pTwoAssociatedPlane->GetTopAssociatedPlaneId())
        return true;

    double height = pTwoAssociatedPlane->ComputeHeight();
    if (height <= 0)
        return true;

    IElementParameters *pParam = pElement->GetElementParameters();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pParam != nullptr, L"无效pParam",L"GDMPLab",L"2024-03-30");

    OwnerPtr<IParameter> heightParam = pParam->GetParameterByUid(PARAMETER_UID(ColumnHeightBuiltInParameter));
    DBG_WARN_AND_RETURN_FALSE_UNLESS(heightParam, L"没有高度参数",L"GDMPLab",L"2024-03-30");

    heightParam->SetValueAsDouble(height);
    return pParam->SetParameter(heightParam.get());
}
